package com.yh.service;

import com.yh.dao.HouseDao;
import com.yh.dao.impl.HouseDaoimpl;
import com.yh.model.HouseModel;
import com.yh.util.Page;

import java.util.List;

public class HouseService {
    private HouseDao houseDao = new HouseDaoimpl();

    public int addHouse(HouseModel houseEntity){
        return houseDao.addHouse(houseEntity);
    }
    public Page<HouseModel> find(int first, int end){
        int count = houseDao.count();
        System.out.println(count);
        int totalPage = 0;
        if (first < 1){
            first = 1;
        }
        totalPage = count % end == 0 ? (count/end) : (count/end+1);
        if (first>=totalPage){
            first = totalPage;
        }
        return new Page<>(first,totalPage,end,houseDao.allHouse((first-1)*end,end));
    }
    public int delHouse(int hid){
        return houseDao.delHouse(hid);
    }
    public HouseModel findoneHouse(int hid){
        return houseDao.findHouse(hid);
    }
    public List<HouseModel> likeHouse(String title,String type,String area,String price,String street){
        return houseDao.likeHouse(title, type, area, price, street);
    }
    public int updateHouse(HouseModel houseEntity){
        return houseDao.updHouse(houseEntity);
    }
    public Page<HouseModel> userHouse(int id,int first,int end){
        int count = houseDao.userHousecount(id);
        int totalPage = 0;
        if (first < 1){
            first = 1;
        }
        totalPage = count % end == 0 ? (count/end) : (count/end+1);
        if (first>=totalPage){
            first = totalPage;
        }
        return new Page<>(first,totalPage,end,houseDao.userHouses(id,(first-1)*end,end));
    }
}
